/*
 * Configuracoes.java
 *
 * Created on 22 de Maio de 2007, 00:38
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.*;

import org.jerm.Interface.JGraph_Diagrama.Aresta;
import org.jerm.Interface.JGraph_Diagrama.Entidade;
import org.jerm.Interface.JGraph_Diagrama.Heranca;
import org.jerm.Interface.JGraph_Diagrama.MyGraph;
import org.jerm.Interface.JGraph_Diagrama.MyModel;
import org.jerm.Interface.JGraph_Diagrama.Relacionamento;
import org.jerm.Mapeamento.Mapear;
import org.jerm.Mapeamento.Atributos.Atributo;
import org.jerm.Mapeamento.Atributos.AtributoSerial;
import org.jerm.Mapeamento.Atributos.FK;
import org.jerm.Mapeamento.Tabelas.Tabela;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.Port;

/**
 *
 * @author Crepaldi
 */
public class Tabelas extends JPanel
{
    private MyGraph graph;
    protected Principal Principal;
    protected JSplitPane split;
    protected JTextArea mer;
    protected JTextArea sql;
    protected JScrollPane mers;
    protected JScrollPane sqls;
    protected JToolBar toolbar;

    private GridBagConstraints c;
    
    /** Creates a new instance of Configuracoes */
    public Tabelas (JGraph graph,Principal Principal)
    {
        super(new BorderLayout());
        //ImageIcon imageIcon;
        //URL iconUrl;
        this.graph = (MyGraph) graph;
        this.Principal=Principal;
        
        this.mer=new JTextArea();
        this.sql=new JTextArea();
        this.mers=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.sqls=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        
        this.split=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        split.setTopComponent(mer);
        split.setBottomComponent(sql);
        split.setResizeWeight(0.3);
        //split.setDividerLocation(0.3);
        split.setOneTouchExpandable(true);
        split.setContinuousLayout(true);
        
        this.toolbar=new JToolBar(JToolBar.HORIZONTAL);
        toolbar.setFloatable(false);
        toolbar.setRollover(true);
        
        //iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/insert_relacionamento.gif");
        //imageIcon = new ImageIcon(iconUrl);
        toolbar.add(new AbstractAction("Gerar") {
                public void actionPerformed(ActionEvent e) {
                    mapear();
                }
        });
        
        //iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/insert_heranca.gif");
        //imageIcon = new ImageIcon(iconUrl);
        toolbar.add(new AbstractAction("Executar") {//, imageIcon
                public void actionPerformed(ActionEvent e) {
                        executar();
                }
        });
        
        add(toolbar, BorderLayout.PAGE_START);
        add(split, BorderLayout.CENTER);
        
    }

    public String getToolTip(){
        return "<html>Blue Wavy Line border art crew:<br>&nbsp;&nbsp;&nbsp;Bill Pauley<br>" +
                "&nbsp;&nbsp;&nbsp;Cris St. Aubyn<br>" +
                "&nbsp;&nbsp;&nbsp;Ben Wronsky<br>" +
                "&nbsp;&nbsp;&nbsp;Nathan Walrath<br>" +
                "&nbsp;&nbsp;&nbsp;Tommy Adams, special consultant</html>";
    }
    
    public void mapear(){
        Mapear m=new Mapear(graph);
        mer.setText(m.getMER());
        sql.setText(m.getSQL(Principal.SGBD_ALVO.getSGBD()));
    }
    
    public void executar(){
        Principal.SGBD_ALVO.executaSQL(sql.getText(),"update");
    }

}
